/**
 * <b>项目名：</b>hclv-web<br/>
 * <b>包名：</b>com.flea.hclv.controller<br/>
 * <b>文件名：</b>HTravelGroupController.java<br/>
 * <b>版本信息：</b>1.0<br/>
 * <b>日期：</b>2018年11月9日 下午7:06:52<br/>
 * <b>COPYRIGHT 2010-2012 ALL RIGHTS RESERVED 虫子科技</b>-版权所有<br/>
 */
package com.flea.hclv.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.flea.hclv.constant.HConstant;
import com.flea.hclv.model.entity.HTravelGroup;
import com.flea.hclv.model.entity.HVisitor;
import com.flea.hclv.model.entity.vo.HTravelGroupChargePersonVo;
import com.flea.hclv.model.entity.vo.VisitorGroupVo;
import com.flea.hclv.service.HTourGroupVisitorService;
import com.flea.hclv.service.HTravelGroupService;
import com.flea.hclv.util.ServerResponse;
import com.flea.system.base.util.StringUtil;
import com.flea.system.bean.FleaPage;
import com.flea.system.entity.OrgDept;
import com.flea.system.platform.session.FleaOrgUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @description 旅游团概念
 * @createTime 2018年11月9日 下午7:06:52
 * @modifyTime
 * @author myflea@163.com
 * @version 1.0
 */
@Controller
@RequestMapping("/flea/hclv/htravelGroup/")
public class HTravelGroupController {
    @Autowired
    private HTravelGroupService travelGroupService;
    @Autowired
    private HTourGroupVisitorService tourGroupVisitorService;

    /**
     * 显示所有状态未出团的团信息，供平台导游管理员进行指派
     * @param paging
     * @param response
     * @param request
     * @return listAllgroup
     */
    @RequestMapping("listAllgroup")
    @ResponseBody
    public ServerResponse<FleaPage<HTravelGroup>> queryAllGroup(FleaPage<HTravelGroup> paging, HttpServletResponse response, HttpServletRequest request/*, @RequestParam("model") String model*/) {
        // 1、团名称
        // 2、班期的时间
        //3、人数范围
        //4、负责人姓名
        //5、团的状态
        String model = request.getParameter("model");
        VisitorGroupVo vo = JSON.parseObject(model,VisitorGroupVo.class);
        vo.setGroupStatus(HConstant.HCLV_VISITOR_GROUP_ISNO);
        ServerResponse<FleaPage<HTravelGroup>> result = null;
        try {
            result = travelGroupService.findAllIsNoGroupForPage(request,vo,paging.getCurPage(),paging.getPageSize());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 显示我的机构的团信息
     * @param paging
     * @param response
     * @param request
     * @return listmygroup
     */
    @RequestMapping("listmygroup")
    @ResponseBody
    public ServerResponse<FleaPage<HTravelGroup>> queryAllMyGroup(FleaPage<HTravelGroup> paging, HttpServletResponse response, HttpServletRequest request/*, @RequestParam("model") String model*/) throws Exception {
        // 1、团名称
        // 2、班期的时间
        //3、人数范围
        //4、负责人姓名
        //5、团的状态
        String model = request.getParameter("model");
        ServerResponse<FleaPage<HTravelGroup>> result = travelGroupService.findMyGroupForPage(request, model, paging.getCurPage(), paging.getPageSize());
        return result;
    }

    @RequestMapping("listMyBackGroup")
    @ResponseBody
    public ServerResponse<FleaPage<HTravelGroup>> listMyBackGroup(FleaPage<HTravelGroup> paging, HttpServletResponse response, HttpServletRequest request/*, @RequestParam("model") String model*/) throws Exception {
        // 1、团名称
        // 2、班期的时间
        //3、人数范围
        //4、负责人姓名
        //5、团的状态
        OrgDept currentUserDept = FleaOrgUtils.getCurrentUserDept(request);
        if (currentUserDept==null){
            return ServerResponse.createByErrorMessage("部门不能为空");
        }
        String serviceType = currentUserDept.getServiceType();
        String model = request.getParameter("model");
        VisitorGroupVo vvo = JSON.parseObject(model,VisitorGroupVo.class);
        //回团 关闭团状态都可以报账
//        VisitorGroupVo vvo = new VisitorGroupVo();
//        vvo.setGroupStatus(HConstant.HCLV_VISITOR_GROUP_ISREBACK);
//        vvo.setGroupCloseStatus(HConstant.HCLV_VISITOR_GROUP_ISCLOSE);
        ServerResponse<FleaPage<HTravelGroup>> result = travelGroupService.findMyBackGroupForPage(paging.getCurPage(), paging.getPageSize(), request, vvo,serviceType);
        return result;
    }

    /**
     * 获取某旅行团的所有的人员信息
     * @param paging
     * @param groupId
     * @return
     */
    @RequestMapping("getAllPersonByGroupId")
    @ResponseBody
    public ServerResponse<FleaPage<HVisitor>> getAllPersonByGroupId(FleaPage<HTravelGroup> paging, @RequestParam("model") String groupId) {
        /**
         * 获取一个团的所有的游客信息
         */
        ServerResponse<FleaPage<HVisitor>> result = tourGroupVisitorService.findAllUserByGroupIdPaging(paging.getCurPage(), paging.getPageSize(), groupId);
        return result;
    }

    /**
     * 导出一个旅行团的所有的游客信息，并导出为xls格式
     * @param groupId
     * @param response
     * @return
     */
    @RequestMapping("getAllPersonByGroupIdOutExcel")
    @ResponseBody
    public ServerResponse<String> getAllPersonByGroupId(@RequestParam("model") String groupId, HttpServletResponse response) {
        /**
         * 获取一个团的所有的游客信息 导入excel
         */
        ServerResponse<String> result = tourGroupVisitorService.findAllUserByGroupId(groupId, response);
        return result;
    }

    /**
     * 业务说明：1、
     * @description 设置团的状态为封团状态
     * @methodName setGroupOk
     * @param
     * @returnType ServerResponse<List < HTravelGroup>>
     * @throw
     */
    @RequestMapping("setGroupOk")
    @ResponseBody
    public ServerResponse<HTravelGroup> setGroupOk(HttpServletRequest request, @RequestParam("model") String groupId) {
        ServerResponse<HTravelGroup> result = null;
        try {
            result = tourGroupVisitorService.updateStatusToOk(groupId, request);
        } catch (Exception e) {
            return ServerResponse.createByErrorMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 业务说明：3、
     * @description 设置团的状态为解封状态
     * @methodName setGroupUnOk
     * @param
     * @returnType ServerResponse<List < HTravelGroup>>
     * @throw
     */
    @RequestMapping("setGroupUnOk")
    @ResponseBody
    public ServerResponse<HTravelGroup> setGroupUnOk(HttpServletRequest request, @RequestParam("model") String groupId) {
        ServerResponse<HTravelGroup> result = null;
        try {
            result = tourGroupVisitorService.updateStatusToUnOk(groupId, request);
        } catch (Exception e) {
            return ServerResponse.createByErrorMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 业务说明：
     * @description 设置团的状态为回团状态
     * @methodName setGroupOk
     * @param
     * @returnType ServerResponse<List < HTravelGroup>>
     * @throw
     */
    @RequestMapping("setGroupIsBack")
    @ResponseBody
    public ServerResponse<HTravelGroup> setGroupIsBack(HttpServletRequest request, @RequestParam("model") String groupId) {
        ServerResponse<HTravelGroup> result = null;
        try {
            result = tourGroupVisitorService.updateStatusIsBack(groupId, request);
        } catch (Exception e) {
            return ServerResponse.createByErrorMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 业务说明：
     * @description 设置团的状态为关闭状态，只有回团状态的情况下才可以进行关闭
     * @methodName setGroupOk
     * @param
     * @returnType ServerResponse<List < HTravelGroup>>
     * @throw
     */
    @RequestMapping("setGroupIsClose")
    @ResponseBody
    public ServerResponse<HTravelGroup> setGroupIsClose(HttpServletRequest request, @RequestParam("model") String groupId) {
        ServerResponse<HTravelGroup> result = null;
        try {
            result = tourGroupVisitorService.updateStatusIsClose(groupId, request);
        } catch (Exception e) {
            return ServerResponse.createByErrorMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 为旅游团添加负责人信息
     * @param request
     * @return
     */
    @RequestMapping("addGroupChargerPerson")
    @ResponseBody
    public ServerResponse<HTravelGroup> addGroupChargerPerson(HttpServletRequest request) {
        String model = request.getParameter("model");
        if (StringUtil.isEmpty(model)) {
            return ServerResponse.createByErrorMessage("参数非法");
        }
        HTravelGroupChargePersonVo pvo = JSONObject.parseObject(model, HTravelGroupChargePersonVo.class);
        ServerResponse result = null;
        try {
            result = tourGroupVisitorService.addGroupChargePerson(request, pvo);
        } catch (Exception e) {
            return ServerResponse.createByErrorMessage(e.getMessage());
        }

        return result;
    }


}
